package com.ruihu.admin.module.business.tasknode.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruihu.admin.common.constant.JudgeEnum;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.module.business.tasknode.dao.NodeInstProcessDefectDao;
import com.ruihu.admin.module.business.tasknode.domain.dto.*;
import com.ruihu.admin.module.business.tasknode.domain.entity.NodeInstProcessDefectEntity;
import com.ruihu.admin.module.business.tasknode.domain.vo.NodeInstProcessDefectExcelVO;
import com.ruihu.admin.module.business.tasknode.domain.vo.NodeInstProcessDefectVO;
import com.ruihu.admin.util.BeanUtil;
import com.ruihu.admin.util.PageUtil;
import com.ruihu.admin.util.RequestTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * [  ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2023-02-22 11:44:41
 * @since JDK1.8
 */
@Service
public class NodeInstProcessDefectService {

    @Autowired
    private NodeInstProcessDefectDao nodeInstProcessDefectDao;

    /**
     * 根据id查询
     */
    public NodeInstProcessDefectEntity getById(Long id){
        return  nodeInstProcessDefectDao.selectById(id);
    }

    /**
     * 分页查询
     * @author Tony
     * @date 2023-02-22 11:44:41
     */
    public ResponseDTO<PageResultDTO<NodeInstProcessDefectVO>> queryByPage(NodeInstProcessDefectQueryDTO queryDTO) {
        Page page = PageUtil.convert2QueryPage(queryDTO);
        List<NodeInstProcessDefectVO> voList = nodeInstProcessDefectDao.queryByPage(page, queryDTO);
        page.setRecords(voList);
        PageResultDTO<NodeInstProcessDefectVO> pageResultDTO = PageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 添加
     * @author Tony
     * @date 2023-02-22 11:44:41
     */
    public ResponseDTO<String> add(NodeInstProcessDefectAddDTO addDTO) {
        NodeInstProcessDefectEntity entity = BeanUtil.copy(addDTO, NodeInstProcessDefectEntity.class);
        nodeInstProcessDefectDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author Tony
     * @date 2023-02-22 11:44:41
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(NodeInstProcessDefectUpdateDTO updateDTO) {
        NodeInstProcessDefectEntity entity = BeanUtil.copy(updateDTO, NodeInstProcessDefectEntity.class);
        nodeInstProcessDefectDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author Tony
     * @date 2023-02-22 11:44:41
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        nodeInstProcessDefectDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author Tony
     * @date 2023-02-22 11:44:41
     */
    public List<NodeInstProcessDefectExcelVO> queryAllExportData(NodeInstProcessDefectQueryDTO queryDTO) {
        return nodeInstProcessDefectDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author Tony
     * @date 2023-02-22 11:44:41
     */
    public List<NodeInstProcessDefectExcelVO> queryBatchExportData(List<Long> idList) {
        return nodeInstProcessDefectDao.queryBatchExportData(idList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateDefectType(Long subTaskId, Long taskNodeInstId, List<DefectTypeDTO> defectTypeList) {
        // 处理缺陷类型
        NodeInstProcessDefectQueryDTO queryDTO = new NodeInstProcessDefectQueryDTO();
        queryDTO.setSubTaskId(subTaskId);
        queryDTO.setTaskNodeInstId(taskNodeInstId);
        queryDTO.setProcessRelationIdIsNull(JudgeEnum.YES.getValue());
        List<NodeInstProcessDefectDTO> nodeInstProcessDefectDTOList = this.nodeInstProcessDefectDao.queryByList(queryDTO);
        Map<Long, NodeInstProcessDefectDTO> existsNodeInstProcessDefectMap = nodeInstProcessDefectDTOList.stream()
                .collect(Collectors.toMap(NodeInstProcessDefectDTO::getDefectTypeId, e -> e));
        // 保存新的缺陷类型
        defectTypeList.forEach(o -> {
            if (!existsNodeInstProcessDefectMap.containsKey(o.getDefectTypeId())) {
                NodeInstProcessDefectEntity nodeInstProcessDefectEntity = new NodeInstProcessDefectEntity();
                nodeInstProcessDefectEntity.setSubTaskId(subTaskId);
                nodeInstProcessDefectEntity.setTaskNodeInstId(taskNodeInstId);
                nodeInstProcessDefectEntity.setDefectTypeId(o.getDefectTypeId());
                nodeInstProcessDefectEntity.setDefectName(o.getDefectName());
                nodeInstProcessDefectEntity.setCreateBy(RequestTokenUtil.getThreadLocalUser().getRequestUserId());
                nodeInstProcessDefectEntity.setCreateTime(new Date());
                this.nodeInstProcessDefectDao.insert(nodeInstProcessDefectEntity);
            }
        });

        // 删除没有选中的缺陷类型
        Set<Long> defectTypeIdSet = defectTypeList.stream().collect(Collectors.mapping(DefectTypeDTO::getDefectTypeId, Collectors.toSet()));
        List<Long> delIdList = new ArrayList<>();
        nodeInstProcessDefectDTOList.forEach(e -> {
            if (!defectTypeIdSet.contains(e.getDefectTypeId()))
                delIdList.add(e.getId());
        });
        if (delIdList.size() > 0)
            this.nodeInstProcessDefectDao.deleteBatchIds(delIdList);
    }
}
